<?php
class NewsAdminForm extends Form
{
	function __construct()
	{				
		Form::Form('NewsAdminForm');
		
		$this->link_css("style/aznet/ui.datepicker.css");
		$this->link_js("javascript/jquery/ui.datepicker.js");
	}
	
	function draw()
	{
		global $display;
		
		AZLib::getNewsCats();
		
		$display->add('url_add',Url::build_current(array('cmd'=>'add')));
		$display->add('msg',$this->showFormErrorMessages(1));
		
		
		$news_catid		=	Url::get('news_catid',0);
		$status			=	Url::get('status',3);
		$date_begin		=	Url::get('date_begin');
		$date_end		=	Url::get('date_end');
		$keyword		=	Url::get('searchtext');
		$hot			=	Url::get('hot',1);
		
		for($i=1; $i<=3; $i++)
		$display->add('selected_'.$i,'');
		
		$display->add('selected_'.Url::get('status',3),'selected');

		for($i=1; $i<=2; $i++)
		$display->add('tinnhanh_'.$i,'');
		$display->add('tinnhanh_'.Url::get('tinnhanh',2),'selected');

		$condition=' 1 ';
		
		
		if ($news_catid)
		{
			$condition.= " AND news_catid =$news_catid ";
		}
		if ($keyword!='')
		{
			$condition.= " AND ((title like '%$keyword%') OR (brief like '%$keyword%')) ";
		}
		
		$created_time_from	= 0;
		$created_time_to	= 0;
		
		if($date_begin)
		{
			$date_arr = explode("-",$date_begin);
			if(isset($date_arr[0]) && isset($date_arr[1]) && isset($date_arr[2]))
			$created_time_from = mktime(0,0,0,(int)$date_arr[1],(int)$date_arr[0],(int)$date_arr[2]);
		}
		
		if($date_end)
		{
			$date_arr = explode("-",$date_end);
			if(isset($date_arr[0]) && isset($date_arr[1]) && isset($date_arr[2]))
			$created_time_to = mktime(23,59,59,(int)$date_arr[1],(int)$date_arr[0],(int)$date_arr[2]);
		}
		
		
		
		if ($created_time_from)
		{
			$condition.= ' AND  time_created >='.$created_time_from;
		}
		
		if ($date_end)
		{
			$condition.= " AND  time_created <=$created_time_to";
		}
		
		if ($status!= 3)
		{
			$condition.= ' and  status='.$status;
		}
		
		if ($hot == 2)
		{
			$condition.= ' and  hot='.$hot;
		}
		
		$cat_arr = array(0=>"--Danh mục tin--");
		
		if(CGlobal::$allNewsCategories)
		{
			foreach (CGlobal::$allNewsCategories as $news_cat)
			{
				$cat_arr[$news_cat['id']] = "[{$news_cat['id']}] ".$news_cat['name'].($news_cat['public'] == 0 ? ' (Đã ẩn)':'');
			}
		}//$news_catid
		
		$display->add('news_cat_option',AZLib::getOption($cat_arr,$news_catid));
		
		$display->add('hot_option',AZLib::getOption(array('1'=>"Tất cả",2=>"Tin Hot"),$hot));
		$display->add('status_option',AZLib::getOption(array(3=>"Tất cả",1=>"Hiện",2=>"Ẩn"),$status));

		$display->add('keyword',	$keyword);
		$display->add('date_begin',$date_begin);
		$display->add('date_end',	$date_end);
		
		$totalRows=DB::count("news",$condition);
		$limit ='';
		$pagging = AZPagging::pagingSE($limit,$totalRows,20,10,'page_no',"tin");
		//$pagging = AZPagging::paging_list($limit,20,'page_no');
		
		$items = array();
		
		$re = DB::query('SELECT * FROM news WHERE '.$condition." ORDER BY id DESC ".$limit,__LINE__.__FILE__,false,false,false,true);
		
		if ($re)
		{
			while ($item = mysql_fetch_assoc($re))
			{
				$item['admin_link'] 	= News::admin_link($item);
				
				if($item['image'])
				{
					$item['image'] 			= AZLib::getImageThumb($item['image'],150,0,0,$item['img_server']);
				}
				
				$item['cat'] 			= "[{$item['news_catid']}] " . @ CGlobal::$allNewsCategories[$item['news_catid']]['name'];
				$item['time_created'] 	= date('d/m/Y', $item['time_created']);
				$item['brief'] 			= stripslashes($item['brief']);
				$item['title'] 			= stripslashes($item['title']);
				$item['url'] 			= Url::build('news_detail',array('news_catid'=>$item['news_catid'],'news_id'=>$item['id']));
				
				
				$items[$item['id']] = $item;
			}
		}
		
		$display->add('items',$items);
		$display->add('pagging',$pagging);
		
		$this->beginForm();
		$display->output("NewsAdmin");
		$this->endForm();
	}
}
?>